
package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.*;
import com.example.shengyin.service.AdminService;
import com.example.shengyin.service.SurveyQuesService;
import com.example.shengyin.service.SurveyService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * AdminController
 * 
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/survey")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(value = "问卷调查")
//redis缓存用
public class SurveyController
{
    @Autowired
    private SurveyService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SurveyQuesService surveyQuesService;

    /**
     * 查询Admin列表
     */


    @ApiOperation("分页查询")
    @GetMapping("/preview/{id}")
    public Result preview(@PathVariable(value = "id") Long id)
    {
        System.out.println("id:"+id);
        Survey select = service.select(id);
        if(select==null)
            return Result.success("没有数据");
        List<SurveyQues> getlist = surveyQuesService.getlist(select.getChoseques());
        System.out.println("getlist"+JSON.toJSONString(getlist));
        String chose = select.getChoseanswer();
        if(chose!=null)
        {
            String[] split = chose.split(",");
            for (int i = 0; i < split.length; i++) {
                getlist.get(i).setChoice(split[i]);
            }
        }
        select.setQues(getlist);
        System.out.println("Result"+JSON.toJSONString(select));
        return Result.success(select);
    }



    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result list(Page page,
                       @ApiParam(name = "phone",value = "手机号") @RequestParam(required = false) String phone,
                       @ApiParam(name = "stype",value = "类别") @RequestParam(required = false) Long stype)
    {
        System.out.println("page:"+page);
        List<Survey> list = service.list(page,phone,stype);
        int count = service.count(phone,stype);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }
    @ApiOperation("单个查找")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Survey select = service.select(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("查找自己的")
    @GetMapping(value = "/selectus")
    public Result selectus(HttpServletRequest request)
    {
        System.out.println(request.getHeader("Authorization"));
        User admin = (User) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin==null)
            return Result.error("请先登录");
        List<Survey> selectus = service.selectus(admin.getId());
        if(selectus==null)
            return Result.error("没有找到数据");
        return Result.success(selectus);
    }
    @ApiOperation("添加")
    @PostMapping("/add")
    public Result insert(@ApiParam(name = "stype",value = "类别") @RequestParam(required = false) Long stype,@ApiParam(name = "number",value = "数目") @RequestParam(required = true) int number,HttpServletRequest request)
    {
        Survey survey=new Survey();
        String authorization = request.getHeader("Authorization");
            survey.setPhone("18654778599");
            survey.setUserid(Long.valueOf(1) );
        if(number<=0)
            return Result.error("题目的数量不能小于0");
        List<Long> list = surveyQuesService.listid(stype);
        if(number>list.size())
            return Result.error("题目的数量不够");
        Collections.shuffle(list);
        Collections.shuffle(list);
        Collections.shuffle(list);
        List<Long> longs = list.subList(0, number);
        Collections.sort(longs);
        System.out.println(JSON.toJSONString(longs));
        String ids=StrUtil.ArreyListtoString(longs);
        List<SurveyQues> listsome = surveyQuesService.listsome(stype, ids);
        survey.setChoseques(ids);
        survey.setStype(stype);
        int insert = service.insert(survey);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success(listsome,"添加成功");
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(Survey dAdmin)
    {
        int insert = service.update(dAdmin);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
    @ApiOperation("删除单个")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id)
    {
        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("删除多个")
	@GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
}
